package com.coszero.ui.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.OvershootInterpolator;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.coszero.utils.utils.DensityUtil;


/**
 * Desc:悬浮可拖动控件
 *
 * @author coszero
 * Email:xmqian93@163.com
 * Date:2023/02/27 13:38
 * @link（https://blog.csdn.net/qq_40976230/article/details/100786247）
 */
public class MoveView extends FrameLayout {
    private int lastX;
    private int lastY;
    private float screenWidth;
    private int top,left,right, bottom;

    public MoveView(@NonNull Context context) {
        this(context, null);
    }

    public MoveView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MoveView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //得到屏幕总宽度
        screenWidth = dm.widthPixels;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        Log.i("####屏幕刷新", "change=" + changed + " left=" + left + " top=" + top + " right=" + right + " bottom=" + bottom + " screenWidth=" + screenWidth + "  wegditWidth=" + getWidth());
        super.onLayout(false, this.left, this.top, this.right, this.bottom);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX = x - lastX;
                int offsetY = y - lastY;
                //第一种方法
                layout(getLeft() + offsetX,
                        getTop() + offsetY,
                        getRight() + offsetX,
                        getBottom() + offsetY);
                //第二种方法
                //offsetLeftAndRight(offsetX);
                //offsetTopAndBottom(offsetY);
                break;
            case MotionEvent.ACTION_UP:
//                adsorbAnim(event.getRawX(), event.getRawY());
                layout(getLeft(),
                        getTop(),
                        getRight(),
                        getBottom());
                top=getTop();
                left = getLeft();
                right=getRight();
                bottom =getBottom();
                break;
        }
        return super.onTouchEvent(event);
    }

    private void adsorbAnim(float rawX, float rawY) {
        //靠顶吸附
        if (rawY <= DensityUtil.dip2px(getContext(), 200)) {//注意rawY包含了标题栏的高
            animate().setDuration(400)
                    .setInterpolator(new OvershootInterpolator())
                    .yBy(-getY() - getHeight() / 2.0f)
                    .start();
        } else if (rawX >= screenWidth / 2) {//靠右吸附
            float moveX = screenWidth - getX() - getWidth()/* / 2.0f*/;
            //防止移出屏幕外
            if (moveX > screenWidth - getWidth()) {
                moveX = screenWidth - getWidth();
            }
            animate().setDuration(400)
                    .setInterpolator(new OvershootInterpolator())
                    .xBy(moveX)
                    .start();
        } else {//靠左吸附
            ObjectAnimator animator = ObjectAnimator.ofFloat(this, "x", getX(), -getWidth() / 2.0f);
            animator.setInterpolator(new OvershootInterpolator());
            animator.setDuration(400);
            animator.start();
        }
    }
}
